package com.eightbears.beboy.base.view.popupwindow;

import android.app.Activity;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eightbears.beboy.R;
import com.eightbears.beboy.adapter.PaymentAdapter;
import com.eightbears.beboy.adapter.TagPopAdapter;
import com.eightbears.beboy.base.util.CommonUtils;
import com.eightbears.beboy.base.util.ErrorUtils;
import com.eightbears.beboy.base.util.GsonUtil;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.util.ShowToast;
import com.eightbears.beboy.base.util.okhttp.OkHttpUtils;
import com.eightbears.beboy.base.util.okhttp.callback.StringCallback;
import com.eightbears.beboy.base.view.basepopu.BasePopupWindow;
import com.eightbears.beboy.base.view.layout.MyLinearLayoutManager;
import com.eightbears.beboy.entity.PayInfoEntity;
import com.eightbears.beboy.entity.PayTypeEntity;
import com.eightbears.beboy.entity.TagsEntity;
import com.eightbears.beboy.entity.WeChatPayEntity;
import com.eightbears.beboy.entity.status.Status_Error;
import com.eightbears.beboy.ui.user.activity.PaymentActivity;
import com.google.gson.Gson;
import com.socks.library.KLog;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

import static com.eightbears.beboy.base.util.CommonAPI.CONNECTION_STATUS_SUCCESS;
import static com.eightbears.beboy.base.util.CommonAPI.KEY_HTTP_HEADER;
import static com.eightbears.beboy.base.util.CommonAPI.URL_ADDSHOPTECHNICIAN_TAGS;
import static com.eightbears.beboy.base.util.CommonAPI.URL_PAY_ALIPAY;
import static com.eightbears.beboy.base.util.CommonAPI.URL_PAY_MYPAY;
import static com.eightbears.beboy.base.util.CommonAPI.URL_PAY_WEIXIN;
import static com.eightbears.beboy.base.util.CommonAPI.WECHAT_APP_ID;
import static com.eightbears.beboy.entity.PayTypeEntity.TYPE_ALIPAY;
import static com.eightbears.beboy.entity.PayTypeEntity.TYPE_MYPAY;
import static com.socks.library.KLog.e;


public class PaySelectPopup extends BasePopupWindow {
    private String payPlatform = TYPE_MYPAY;
    private View popupView;
    private RecyclerView rv_pay_info;
    private Button btn_send;
    private TextView tv_hongbao_get;
    private LinearLayout ll_bottom_menu;
    private OnClickItemListener mOnClicktemListener;
    private TagPopAdapter mTagPopAdapter;
    private PaymentAdapter mPaymentAdapter;
    private ImageView mIv_clear;

    public PaySelectPopup(Activity context) {
        super(context);
        bindEvent();
        initAdapter();
        initData();
    }

    @Override
    public Animation getAnimation() {
        return getDefaultAlphaAnimation();
    }

    @Override
    public AnimationSet getAnimationSet() {
        return null;
    }

    @Override
    public View getInputView() {
        return null;
    }

    @Override
    public View getDismissView() {
        return popupView.findViewById(R.id.click_to_dismiss);
    }

    @Override
    public View getPopupView() {
        popupView = LayoutInflater.from(mContext).inflate(R.layout.view_pay_popup, null);
        return popupView;
    }

    @Override
    public View getAnimaView() {
        return popupView.findViewById(R.id.popup_anima);
    }

    private void bindEvent() {
        if (popupView != null) {
            rv_pay_info = (RecyclerView) popupView.findViewById(R.id.rv_pay_info);
            btn_send = (Button) popupView.findViewById(R.id.btn_send);
            mIv_clear = (ImageView) popupView.findViewById(R.id.iv_clear);
        }
    }


    private void initAdapter() {
        rv_pay_info.setLayoutManager(new MyLinearLayoutManager(mContext));
        mPaymentAdapter = new PaymentAdapter();
        List<PayTypeEntity> list = PayTypeEntity.getPayList();
        mPaymentAdapter.setNewData(list);
        mPaymentAdapter.checked(0);
        rv_pay_info.setAdapter(mPaymentAdapter);

        mPaymentAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.ll_item:
                        mPaymentAdapter.checked(position);
                        KLog.e(view.getTag().toString());
                        payPlatform = view.getTag().toString();
                        break;
                }
            }
        });

        btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnClicktemListener.onClickItem(payPlatform, v);
                dismiss();
            }
        });

        mIv_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });


    }

    private void initData() {
        //获取accesstoken
        OkHttpUtils.get().url(URL_ADDSHOPTECHNICIAN_TAGS)
                .addHeader(KEY_HTTP_HEADER, "Bearer " + SPUtil.getAccessToken())
                .addParams("type", new Gson().toJson(2))
                .build().execute(new StringCallback() {
            //TODO
            @Override
            public void onResponse(String response) {
                KLog.e(response);
                try {
                    TagsEntity tagsEntity = GsonUtil.fromJson(response, TagsEntity.class);
                    List<TagsEntity.DataBean> data = tagsEntity.getData();
                    mTagPopAdapter.setNewData(data);
                    rv_pay_info.setAdapter(mPaymentAdapter);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Call request, Exception e) {
                KLog.e(e.getMessage());
                Status_Error status_error = GsonUtil.fromJson(e.getMessage(), Status_Error.class);
                ErrorUtils.alertError(mContext, status_error);
            }
        });
    }

    public interface OnClickItemListener {
        void onClickItem(String payPlatform, View v);
    }

    public void setOnClickItemListener(OnClickItemListener onClickItemListener) {
        this.mOnClicktemListener = onClickItemListener;
    }


}
